
package com.wallj.videolibrary.player.renderer;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.wallj.videolibrary.filters.SpWaterMarkFilter;
import com.wallj.videolibrary.player.WaterMarkEntity;
import com.wallj.videolibrary.utils.OpenGLUtils;
import com.wallj.videolibrary.utils.Text2BitmapUtils;

import javax.microedition.khronos.opengles.GL10;

public class WaterMarkRenderer extends PixelRenderer {

    private WaterMarkEntity waterMarkEntity;

    private Bitmap waterMark;

    private int waterMarkTextureId = OpenGLUtils.GL_NOT_TEXTURE;

    private SpWaterMarkFilter glImageFilter;

    private boolean mHasWaterMarkChanged = false;

    public WaterMarkRenderer(Context context) {
        super(context);
    }

    public void showWaterMark(WaterMarkEntity waterMarkEntity) {
        this.waterMarkEntity = waterMarkEntity;
        if (this.waterMarkEntity != null && !TextUtils.isEmpty(this.waterMarkEntity.ad)) {
            mHasWaterMarkChanged = true;
        }
    }

    public void resume() {
        if (this.waterMarkEntity != null && !TextUtils.isEmpty(this.waterMarkEntity.ad)) {
            mHasWaterMarkChanged = true;
        }
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        super.onDrawFrame(gl);
        if (mIncommingWidth > 0 && mIncommingHeight > 0 && this.waterMarkEntity != null
            && !TextUtils.isEmpty(this.waterMarkEntity.ad)) {
            if (waterMark == null && mHasWaterMarkChanged) {
                waterMark = Text2BitmapUtils.createBitmap(mContext,
                    (int) (mIncommingWidth * waterMarkEntity.w),
                    (int) (mIncommingHeight * waterMarkEntity.h), waterMarkEntity.ad);
                waterMarkTextureId = OpenGLUtils.createTexture(waterMark, waterMarkTextureId);
                if (mPixelationFilter != null) {
                    mPixelationFilter.setPixels(waterMarkEntity.x, waterMarkEntity.y,
                        waterMarkEntity.w, waterMarkEntity.h);
                }
                mHasWaterMarkChanged = false;
            }
            if (glImageFilter == null) {
                glImageFilter = new SpWaterMarkFilter(mContext);
                glImageFilter.onInputSizeChanged(mIncommingWidth, mIncommingHeight);
                glImageFilter.onDisplaySizeChanged((int) (mSurfaceWidth * waterMarkEntity.w),
                    (int) (mSurfaceHeight * waterMarkEntity.h));
            }
            glImageFilter.drawFrame(waterMarkTextureId, mVertexBuffer, mTextureBuffer,
                (int) (mSurfaceWidth * waterMarkEntity.x),
                (int) (mSurfaceHeight * waterMarkEntity.y));
        }
    }

    @Override
    public void release() {
        super.release();
        if (waterMark != null) {
            waterMark.recycle();
            waterMark = null;
        }
        if (waterMarkTextureId != OpenGLUtils.GL_NOT_TEXTURE) {
            OpenGLUtils.deleteTexture(waterMarkTextureId);
            waterMarkTextureId = OpenGLUtils.GL_NOT_TEXTURE;
        }
        if (glImageFilter != null) {
            glImageFilter.release();
            glImageFilter = null;
        }
    }
}
